<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib  prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!doctype html>
<html lang="zh-CN">
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
		<meta charset="utf-8"/>
		<meta http-equiv="Cache-Control" content="no-store" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Expires" content="0" />

	    <link href="${ctx}/static/css/report.css" rel="stylesheet" type="text/css" />
	    <link href="${ctx}/static/jquery-autocomplete/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
	    <style type="text/css">
	    	span.error{
	    		color:red;
	    		//border:1px solid red;
	    	}
	    	span.ok{
	    		color:#000;
	    		border:none;
	    	}

	    </style>
	</head>
	
	<body>
		   <form id="report_form" name="report_form" method="post" action="service.do?method=saveReport">
                  <!--report的隐藏值，begin-->
                  <input type="hidden" name="reportType" value="${report.reportType}">
                  <input type="hidden" name="reportTypeCode" value="${report.reportTypeCode}">
                  <input type="hidden" name="reportId" id="reportId" value="${report.reportId}">
                  <input type="hidden" name="checkStatus" id="checkStatus" value="${report.checkStatus}">
                  <input type="hidden" name="cityName" value="${report.cityName}">
                  <input type="hidden" name="reportUserId" value="${report.reportUserId}">
                  <input type="hidden" name="reportOperatorId" value="${report.reportOperatorId}">
                  <input type="hidden" name="reportProxyId" value="${report.reportProxyId}">
                  <input type="hidden" name="reportAreaId" id="reportAreaId" value="${report.reportAreaId}">
                  <input type="hidden" name="reportUnitIndustry" value="${report.reportUnitIndustry}">
				  <input type="hidden" name="reportReferenceId" value="${report.reportReferenceId}">
                  <input type="hidden" name="reportTimetype" value="${report.reportTimetype}">
                  <input type="hidden" name="reportYear" value="${report.reportYear}">
                  <input type="hidden" name="reportMonth" id="reportMonth" value="${report.reportMonth}">
                  <input type="hidden" name="reportQuarter" id="reportQuarter" value="${report.reportQuarter}">
                  <input type="hidden" name="reportYearTime" value="${report.reportYearTime}">
                  <input type="hidden" name="reportVersion" value="${report.reportVersion}">
                  <input type="hidden" name="reportUnitIndustryId" value="${report.reportUnitIndustryId}">
                  <input type="hidden" name="reportTypeId" id="reportTypeId" value="${report.reportTypeId}">
                  <input type="hidden" name="reportEdition" value="${report.reportEdition}">
                  <input type="hidden" name="errorCode" id="errorCode" value="0" />
                  <input type="hidden" name="reportUnit"  value="${report.reportUnit }" />
                  <input type="hidden" name="unitId" id="unitId" value="${report.unitId }" />
                <!--report的隐藏值，end-->

		   		<div class="">
		   			<div >
			   			${reportTemplate}
			   		</div>
			   			<div id="report-operate">


							<div>
							<table id="remark-table" width="100%" border="0">
									<tr>
							           <td>
							       			备注：
							        	</td>
							        	<td>
							        		审核意见：
							        	</td>
							       </tr>
							       <tr>
							       		<td><textarea name="reportRemark">${report.reportRemark }</textarea></td>
							           <td>
							           	  <c:choose>
							           	  	<c:when test="${ report.reportId == null}">
							           	  		<textarea name="checkComment" readonly="readonly">${report.checkComment }</textarea>
							           	  	</c:when>
							           	  	<c:otherwise>
							           	  		<textarea name="checkComment" readonly="readonly">${report.checkComment }</textarea>
							           	  	</c:otherwise>
							           	  </c:choose>

							           </td>
							       </tr>
							        <tr>
							          <td align="center" colspan="2">
							          <!--0:新增未提交      3：省级驳回企业提交  -->
							          <c:if test="${report.checkStatus == 0 or report.checkStatus == 3}">
							          	 <input type="hidden" name="commit" id="commit" value="0"/>
							          	  <c:if test="${bill=='1'}">
							          	   <input type="submit"  name="commitButton" class="button_01_lef" commit="1" action="service.do?method=submitReport"  value="提交" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          	</c:if>
							          	  <c:if test="${bill!='1'}">
							          	  <input type="submit"  name="saveButton" class="button_01_lef"  commit="0" action="service.do?method=saveReport"  value="保存" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          	 <input type="submit"  name="commitButton" class="button_01_lef" commit="1" action="service.do?method=submitReport"  value="提交" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          	 </c:if>
							          	 </c:if>
							          	<!--   <c:if test="${report.reportId!=null && reportType.dataSummarization == 1}">
							          	 <input type="submit"  name="reloadReportAccounting" class="button_01_lef"  action="service.do?method=reloadReportAccounting"  value="重新汇总" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          	 </c:if> -->
							          <!--2:省审核通过企业上报未提交总局      7：总局驳回 -->
							          <c:if test="${(report.checkStatus == 2 or report.checkStatus == 7 ) and report.reportUnitIndustry == ''} ">
							          	<input type="hidden"  name="commit" id="commit" value="0"/>
							           <c:if test="${bill=='1'}">
							          	   <input type="submit"  name="commitButton" class="button_01_lef" commit="1" action="service.do?method=submitReport"  value="提交" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          	</c:if>
							          	  <c:if test="${bill!='1'}">
							          	  <input type="submit"  name="saveButton" class="button_01_lef"  commit="0" action="service.do?method=saveReport"  value="保存" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          	 <input type="submit"  name="commitButton" class="button_01_lef" commit="1" action="service.do?method=submitReport"  value="提交" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          	 </c:if>
							          	  </c:if>
							          <!--  <input type="button"  name="closeButton" class="button_01_lef"  onclick="javascript:window.close();" value="退出" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-->

							          <c:if test="${report.checkStatus == 1 or  report.checkStatus == 6}">
							           	<input type="hidden"  name="saveAudit" class="button_01_lef"  onclick="javascript:document.getElementById('commit').value='0';document.getElementById('report_form').action='service.do?method=saveAudit';" value="保存修改" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
							          </c:if>
							          <c:if test="${runMode=='LAN'}">
										<input type="button"  name="showDetailProButton" class="button_01_lef" id="showDetailButton" value="汇总详情" />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
									  </c:if>
							          </td>
							        </tr>
							      </table>
							</div>
			   			</div>
		   			</div>

		   </form>
		   <div id="showDetailContainer"></div>
	</body>
<script  type="text/javascript" src="${ctx}/static/jquery/jquery-1.8.3.js"></script>
<script  type="text/javascript" src="${ctx}/static/jquery-validation/jquery.validate.js"></script>
<script  type="text/javascript" src="${ctx}/static/jquery-validation/additional-methods.js"></script>
<script  type="text/javascript" src="${ctx}/static/jquery-validation/messages_zh.js"></script>
<script  type="text/javascript" src="${ctx}/static/My97DatePicker/WdatePicker.js"></script>
<script  type="text/javascript">
<c:if test="${reportType.dataType eq 2}">
	$('#report-body td input:text').each(function(){
		this.id = "f" + Math.floor(Math.random() * 5000);
	});
</c:if>

	$('input[type="submit"]').click(function(){
		$this = $(this);
		$('#commit').val($this.attr('commit'));
		$('#report_form').attr('action',$this.attr('action'));
	});
	//必填指标
	var industryIndicators='${industryIndicators}';
	var industryIndicatorMap = {};
	if(industryIndicators){
		industryIndicatorMap  = $.parseJSON(industryIndicators);
	}
	var lastTimeIndicatorMap = $.parseJSON('${lastTimeIndicatorMap}');
	var validator = $('#report_form').data('lastTimeIndicatorMap',lastTimeIndicatorMap)
			.data('masterColumnStr','${reportType.masterColumn}').validate({
		errorElement:'span',
		errorPlacement: function(error, element) {
			 error.appendTo( element.parent("td"));
		},
		highlight: function(element) {
			$(element).removeClass('ok').addClass('error').css('border','1px solid red');
		},
		success: function(label,element) {
			$(element).removeClass('error').addClass('ok').css('border','1px solid #000');
		},
	    submitHandler: function(form) {
	    	$('input[type="submit"]').attr('disabled','disabled');
			form.submit();
		},
		invalidHandler: function(event, validator) {
			var errorRules = validator.getErrorRule();
			var onlyIncreaseError = true;
			$.each(errorRules,function(key,val){
				if(val.length == 1 && (val[0] == 'increase' || val[0] == 'row-increase')){

				}else{
					onlyIncreaseError = false;
					return false;
				}
			});
			if(onlyIncreaseError){
				if(confirm("存在指标值增幅验证错误，确定提交吗?")){
					$('input[type="submit"]').addClass('cancel');
					$('#report_form').data('validator', null);
					$("#report_form").unbind('validate');
					$('#report_form').submit();
				}else{

				}
			}
		}
	});


	$('#report-body td[indicator-input-type="checkbox"]').on('click',':checkbox',function(){
		var $hiddenField = $(this).parent().find(':hidden');
		var checkValues = $hiddenField.val().split(',');
		var index = checkValues.indexOf(this.value);
		if(this.checked){
			checkValues.push(this.value);
		}else{
			checkValues.splice(index, 1);
		}
		$hiddenField.val(checkValues.join(','));

	});
	
	
	$('#showDetailButton').on('click',function(){
		var index = layer.open({
			  type: 3, 
			  content: '加载中',
			  icon:3
			});
		var param = {
			'areaId'	: $('#reportAreaId').val(),
			'timeDate' : $('#reportMonth').val(),
			'quarterDate' : $('#reportQuarter').val(),
			'reportTypeId' : $('#reportTypeId').val(),
			'unitId' : $("#unitId").val()
		};
		$.post('service.do?method=generelReportsDetail',param).done(function(data){
			layer.close(index);
			$('#showDetailContainer').html(data);			
		}).fail(function(){
			layer.close(index);
			alert('error');
		});
	});
	if(industryIndicatorMap.length){
		$.each(industryIndicatorMap,function(key,val){
			var indicator = val.indicatorId;
			var selector = 'input[type="text"]';
			var ele = $('#report-body table tbody').find('td[indicatorid="' + indicator +'"]').find(selector);
			ele.attr("required", "true");
			ele.css({"background":"#99ffdd"}); 
			ele.css("align", "right");
		});
	$('#report-body td input[type="text"]').each(function(i){
		var name=$(this).parent().attr('indicatorid');
		var flag=false;
			$.each(industryIndicatorMap,function(key,val){
				var indicator = val.indicatorId;
				var start = name.indexOf(indicator);
				if(start == 0){
					flag=true;
					return false;
				}
			});
			if(!flag){
				$(this).attr("readOnly","true");
				$(this).css('border','0px');
			}
		});
		$('#report-body td[indicator-input-type="select"]').each(function(i){
			var name=$(this).parent().attr('indicatorid');
			var flag=false;
				$.each(industryIndicatorMap,function(key,val){
					var indicator = val.indicatorId;
					var start = name.indexOf(indicator);
					if(start == 0){
						flag=true;
						return false;
					}
				});
				if(!flag)
			$(this).empty();
		});
		$('#report-body td[indicator-input-type="radio"]').each(function(i){
			var name=$(this).parent().attr('indicatorid');
			var flag=false;
				$.each(industryIndicatorMap,function(key,val){
					var indicator = val.indicatorId;
					var start = name.indexOf(indicator);
					if(start == 0){
						flag=true;
						return false;
					}
				});
				if(!flag)
			$(this).empty();
		});
		
		$('#report-body td[indicator-input-type="checkbox"]').each(function(i){
			var name=$(this).parent().attr('indicatorid');
			var flag=false;
				$.each(industryIndicatorMap,function(key,val){
					var indicator = val.indicatorId;
					var start = name.indexOf(indicator);
					if(start == 0){
						flag=true;
						return false;
					}
				});
				if(!flag)
			$(this).empty();
		});
	}
</script>
<c:if test="${reportType.dataType eq 1}">
<script  type="text/javascript" src="${ctx}/static/jquery-autocomplete/jquery.autocomplete.min.js"></script>
<script type="text/javascript">

	function _count(obj){
		 var count = 0;
		 for (var k in obj) {
			    if (obj.hasOwnProperty(k)) {
			       count++;
			    }
			}

		    return count;
	}
	var accounting = $.parseJSON('${accounting}');
	$.each(accounting,function(key,val){
		var selector = '#report-body table tbody  td[indicatorid="' + key + '"] input ';
		if(_count(val) == 1 ){
			var $elem = $(selector),elemVal = $elem.val();
			for(var prop in val) {
				if($.trim(elemVal) == ''){
					$elem.val(val[prop]);
				}else{
					$elem.attr('title','汇总数据为:' + val[prop]);
				}
			 }
		}else{
			var datas = [];
			for(var prop in val){
				var obj = {};
				obj.unit = prop;
				obj.indicatorVal = val[prop];
				datas.push(obj);
			}
			$(selector).autocomplete(datas, {
				width: 280,
				minChars: 0,
				max: 12,
				autoFill: true,
				matchContains: "word",
				formatItem: function(row, i, max) {
					return i + "/" + max + ": \"" + row.unit + "\" [" + row.indicatorVal + "]";
				},
				formatMatch: function(row, i, max) {
					return row.unit + " " + row.indicatorVal;
				},
				formatResult: function(row) {
					return row.indicatorVal;
				}

			}).focus(function(){
				$(this).result();
				return false;
			});
		}
	});
</script>
</c:if>
<c:if test="${reportType.dataType eq 2}">
<script type="text/javascript">
	var trTemplate = '<tr>' + $('#report-body > table > tbody > tr:eq(0)').html() + '</tr>';
	$('#add-report-body-row-btn').click(function(){
		var reportBodyRowCount = $('#report-body > table > tbody > tr').size();

		$('#report-body > table > tbody').append(trTemplate);
		$('#report-body > table > tbody > tr').last().find('input').val('');
		$('#report-body > table > tbody > tr').last().find('input[name="rowIndex"]').val(reportBodyRowCount);
		$('#report-body > table > tbody > tr').last().find('input[name="rowType"]').val('11');
		$('#report-body > table > tbody > tr').last().find('input:text').each(function(){
			this.id = "f-" + Math.floor(Math.random() * 5000 + 5000);
		});
		
		initRowHidNum();
	});

	$('#delete-report-body-row-btn').toggle(function(){
		 $(this).text('<隐藏删行>');
		 $('#add-report-body-row-btn').hide();
		 $('#report-body thead').find('tr').each(function(){
		        $(this).find('th').eq(0).before('<th style="width:50px;"> </th>');
		 });
		 $('#report-body tbody').find('tr').each(function(){
		        $(this).find('td').eq(0).before('<td><a href="javascript:;" class="delete-row-btn">删除</a></td>');
		 });
	},function(){
		 $(this).text('<显示删行>');
		 $('#add-report-body-row-btn').show();
		 $('#report-body thead').find('tr').each(function(){
		        $(this).find('th').eq(0).remove();
		 });
		 $('#report-body tbody').find('tr').each(function(){
		        $(this).find('td').eq(0).remove();
		 });

	});

	$('#report-body table').on('click','a.delete-row-btn',function(){
		$(this).parents('tr').remove();
		$('#report-body tbody tr').each(function(i){
			$(this).find('input[name="rowIndex"]').val(i);
		});
		initRowHidNum();
	});
</script>
</c:if>
<script type="text/javascript" src="${ctx}/static/Util.js"></script>
<script type="text/javascript" src="${ctx}/static/report.js"></script>
<script src="${ctx}/static/js/layer3.1.1/layer.js"></script>

</html>
